package display;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;
import java.util.HashMap;

import service.BandaManager;
import domain.Banda;

import service.CDManager;
import domain.CD;

@Controller
@RequestMapping("/banda")
public class BandaController {
 
 @Autowired
 private BandaManager bandaManager;
 
 @Autowired
 private CDManager CDManager;
 
 @RequestMapping(value="/listado", method = RequestMethod.GET)
 public ModelAndView listado() {

   Map<String, Object> myModel = new HashMap<String, Object>();
   myModel.put("bandas", this.bandaManager.getAll());
   return new ModelAndView("listaBandas", "model", myModel);

 }
 
 @RequestMapping(value="/detalleBanda/{pk_banda}", method = RequestMethod.GET)
 public ModelAndView detalleBanda(@PathVariable("pk_banda") String id) {

   ModelAndView modelAndView = new ModelAndView();
   Banda element = this.bandaManager.getById(id);

   if (element == null){
     System.out.println("NULO");
	}else{ 
    modelAndView = new ModelAndView("detalleBanda");
    modelAndView.addObject("banda", element);

		Map<String, Object> myModel = new HashMap<String, Object>();
    myModel.put("CDs", this.CDManager.getAllByBandaID(id));
		modelAndView.addObject("model", myModel);
   }
    return modelAndView;	 
 }
 
 @RequestMapping(value="/actualizarBanda/{pk_banda}", params = { "pk_banda","nombre","genero","anoFormacion","isVigente" }, method = RequestMethod.GET)
 public String actualizarBanda(
     @PathVariable("pk_banda") String id, 
     @RequestParam("nombre") String nombre,
     @RequestParam("genero") String genero,
     @RequestParam("anoFormacion") String anoFormacion,
     @RequestParam("isVigente") String isVigente
     
  ) {
   Banda element = this.bandaManager.getById(id);
   element.setNombre(nombre);
   element.setGenero(genero);
   element.setAnoFormacion(anoFormacion);
   element.setIsVigente(isVigente);   
   return "forward:/banda/listado";
 }
 
 @RequestMapping(value="/agregarBanda", method = RequestMethod.GET)
 public ModelAndView aregar() {
 	return new ModelAndView("agregarBanda");
 }
 
 @RequestMapping(value="/insertarBanda", params = { "nombre","genero","anoFormacion","isVigente"}, method = RequestMethod.GET)
 public String actualizar(
     @RequestParam("nombre") String nombre,
     @RequestParam("genero") String genero,
     @RequestParam("anoFormacion") String anoFormacion,
     @RequestParam("isVigente") String isVigente
  ) {
   Banda element = new Banda();
   element.setNombre(nombre);
   element.setGenero(genero);
   element.setAnoFormacion(anoFormacion);
   this.bandaManager.insert(element);
   return "forward:/banda/listado";
 }
 
  @RequestMapping(value="/eliminarBanda/{pk_banda}", method = RequestMethod.GET)
 public String eliminar(@PathVariable("pk_banda") String id) {
   if (id != null){
	   this.bandaManager.delete(id);
   }
   return "forward:/banda/listado";
 } 

 public void setBandaManager(BandaManager bandaManager) {
   this.bandaManager = bandaManager;
 } 
}